//
//  FGKeyboardResources.m
//  FGKeyboard
//
//  Created by Henry on 2021/6/13.
//

#import "FGKeyboardResources.h"

@implementation FGKeyboardResources

+ (UIImage *)getKeyboardImage:(NSString *)name {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *url = [bundle URLForResource:@"FGKeyboard" withExtension:@"bundle"];
    NSBundle *targetBundle = [NSBundle bundleWithURL:url];
    UIImage *image = [UIImage imageNamed:name inBundle:targetBundle compatibleWithTraitCollection:nil];
    return image;
}

/* 原来
+ (NSString *)getPathWithFace:(NSString *)name {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *bundleURL = [bundle URLForResource:@"FGFace" withExtension:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
    return [[resourceBundle ?: bundle resourcePath] stringByAppendingPathComponent:name];
}

+ (UIImage *)getImageFromFace:(NSString *)name {
    NSString *path = [self getPathWithFace:name];
    return [UIImage imageWithContentsOfFile:path];
}

+ (NSMutableArray *)defaultFaceData {
    NSMutableArray *faceGroups = [NSMutableArray array];
    NSMutableArray *emojiFaces = [NSMutableArray array];
    NSArray *emojis = [NSArray arrayWithContentsOfFile:[self getPathWithFace:@"emoji/emoji.plist"]];
    for (NSDictionary *dic in emojis) {
        FGKeyboardFaceItem *data = [[FGKeyboardFaceItem alloc] init];
        NSString *name = [dic objectForKey:@"face_name"];
        NSString *path = [NSString stringWithFormat:@"emoji/%@", name];
        data.name = name;
        data.path = [self getPathWithFace:path];
//        [self addFaceToCache:data.path];
        [emojiFaces addObject:data];
    }

    if(emojiFaces.count != 0){
        FGKeyboardFaceGroup *emojiGroup = [[FGKeyboardFaceGroup alloc] init];
        emojiGroup.groupIndex = 0;
        emojiGroup.groupPath = [self getPathWithFace:@"emoji/"];
        emojiGroup.faces = emojiFaces;
        emojiGroup.needBackDelete = YES;
        emojiGroup.menuPath = [self getPathWithFace:@"emoji/menu"];
//        [self addFaceToCache:emojiGroup.menuPath];
        [faceGroups addObject:emojiGroup];

    }
    return faceGroups;
}
 */

+ (NSString *)getPathWithFace:(NSString *)name {
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    NSURL *bundleURL = [bundle URLForResource:@"WZMEmoticon" withExtension:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
    return [[resourceBundle ?: bundle resourcePath] stringByAppendingPathComponent:name];
}

+ (UIImage *)getImageFromFace:(NSString *)name {
    NSString *path = [self getPathWithFace:name];
    return [UIImage imageWithContentsOfFile:path];
}

+ (NSMutableArray *)defaultFaceData {
    NSMutableArray *faceGroups = [NSMutableArray array];
    NSMutableArray *emojiFaces = [NSMutableArray array];
    NSArray *emojis = [NSArray arrayWithContentsOfFile:[self getPathWithFace:@"emoji/emoji.plist"]];
    for (NSDictionary *dic in emojis) {
        FGKeyboardFaceItem *data = [[FGKeyboardFaceItem alloc] init];
        NSString *name = [dic objectForKey:@"png"];
        NSString *path = [NSString stringWithFormat:@"emoji/%@", name];
        data.name = [dic objectForKey:@"chs"];
        data.path = [self getPathWithFace:path];
//        [self addFaceToCache:data.path];
        [emojiFaces addObject:data];
    }
    
    if(emojiFaces.count != 0){
        FGKeyboardFaceGroup *emojiGroup = [[FGKeyboardFaceGroup alloc] init];
        emojiGroup.groupIndex = 0;
        emojiGroup.groupPath = [self getPathWithFace:@"emoji/"];
        emojiGroup.faces = emojiFaces;
        emojiGroup.needBackDelete = YES;
        emojiGroup.menuPath = [self getPathWithFace:@"emoji/fg2f942"];
//        [self addFaceToCache:emojiGroup.menuPath];
        [faceGroups addObject:emojiGroup];
        
    }
    return faceGroups;
}

@end
